﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;


namespace HtmlExtractor
{
    /// <summary>
    /// 所有outputpath不为空的结果，为空的是中间结果不加入解析结果集合
    /// </summary>
    public class PageResult:List<IPageItem>
    {
        /// <summary>
        /// 键值，如解析的Uri
        /// </summary>
        public string CurrentUri
        { get; set; }
        public string OutputProcessorName
        { get; set; }
        public PageResult()
        {
            
        }
       
       /// <summary>
       /// 
       /// </summary>
       /// <returns>key:outputpath </returns>
        public IEnumerable<IGrouping<List<string>, IPageItem>> GroupByOutputPath()
        {
           // var t = this.GroupBy(item => item.GetOutputPath1());
            var t= this.GroupBy(item => item.GetOutputPath(),new ListCompare());
            return t;
        }
        /// <summary>
        /// 
        /// </summary>
        public IEnumerable<CompositeItem> GetCompositeGroupResult
        {
            get
            {
                return this.OfType<CompositeItem>();
            }
        }
        public IEnumerable<SimpleItem> GetMatchResult
        {
            get
            {
                return this.OfType<SimpleItem>();
            }
        }

        internal IEnumerable<IGrouping<string, IPageItem>> GroupSimpleByOutputPath()
        {
            throw new NotImplementedException();
        }
        class ListCompare : IEqualityComparer<List<string>>
        {
            public string ToString(List<string> item)
            {
                string split = ",";
                var outpaths = from s in item
                               orderby s
                               select s;
                return outpaths.Aggregate((a, b) => a + split + b);


            }
            #region IEqualityComparer<List<string>> Members

            public bool Equals(List<string> x, List<string> y)
            {
                string xs = ToString(x);
                string ys = ToString(y);
                return xs.Equals(ys, StringComparison.CurrentCulture);
            }

            public int GetHashCode(List<string> obj)
            {
                return ToString(obj).GetHashCode();
            }

            #endregion
        }
    }
}
